﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebServiceTest
{

    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://localhost/AppConfigService/",
                Description = "The Config Service is designed to provide configuration information, previously stored in configuration server")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]    
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class AppConfigService : System.Web.Services.WebService, IAppConfigService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        
        /// <summary>
        /// Get config information for given application in xml
        /// </summary>
        /// <param name="getAppConfigReq">Containe appId and optional getScheme</param>
        /// <returns>Containe config information for given application in xml</returns>
        public GetAppConfigResp GetAppConfig(GetAppConfigReq getAppConfigReq)
        {
            GetAppConfigResp getAppConfigResp = new GetAppConfigResp();

            //get appId and getscheme from request
            //select config information for appId in current config version from DB
            //make response

            return getAppConfigResp;
        }

        /// <summary>
        /// Get complete config information for all applications.
        /// </summary>
        /// <param name="getConfigReq"></param>
        /// <returns>Containe config information for all application in xml</returns>
        public GetConfigResp GetConfig(GetConfigReq getConfigReq)
        {
            GetConfigResp getConfigResp = new GetConfigResp();

            //select config information for all applications in current config version from DB
            //make answer array
            //make response

            return getConfigResp;
        }

        /// <summary>
        /// Set Application Configuration for a specific Application
        /// </summary>
        /// <param name="setAppConfigReq">Containe application xml config </param>
        /// <returns>Containe appid and updated application config version</returns>
        public CommonAppVersionResp SetAppConfig(SetAppConfigReq setAppConfigReq)
        {
            CommonAppVersionResp commonAppVersionResp = new CommonAppVersionResp();
            
            //get appId and config from request
            //update application config in DB
            //update whole config versions
            //update current config version
            //select application version
            //make response

            return commonAppVersionResp;
        }

        /// <summary>
        /// Set configuration information for all applications
        /// </summary>
        /// <param name="setConfigReq">Containe application xml config </param>
        /// <returns>Containe updated config version</returns>
        public CommonVersionResp SetConfig(SetConfigReq setConfigReq)
        {
            CommonVersionResp commonVersionResp = new CommonVersionResp();

            //get scheme from request
            //repeat for all applications in request
            //get appId and config from request
            //validate of config with scheme
            //update application config in DB

            //update whole config versions
            //update current config version
            //select config version
            //make response

            return commonVersionResp;
        }

        /// <summary>
        /// Creates a new application (if provided appid is not already used)
        /// and either sets it’s config to xml provided
        /// </summary>
        /// <param name="addAppReq">Containe appId and config xml </param>
        /// <returns>Containe appid and updated application config version</returns>
        public CommonAppVersionResp AddApp(AddAppReq addAppReq)
        {
            CommonAppVersionResp commonAppVersionResp = new CommonAppVersionResp();

            //get appId
            //add app to DB
            //set application config
            
            return commonAppVersionResp;
        }

        /// <summary>
        /// Remove a specific application from the tables
        /// </summary>
        /// <param name="removeAppReq">Containe appId</param>
        /// <returns>empty response</returns>
        public CommonEmptyResp RemoveApp(RemoveAppReq removeAppReq)
        {

            //get appId
            //set to new config version without app with appId

            return new CommonEmptyResp();
        }

        /// <summary>
        /// Rolls back application configuration to the version provided in request
        /// or previous version if Version parameter was omitted
        /// </summary>
        /// <param name="rollBackAppReq">Containe appId and optional roll back config version</param>
        /// <returns>Containe updated config version</returns>
        public CommonVersionResp RollBackApp(RollBackAppReq rollBackAppReq)
        {
            CommonVersionResp commonVersionResp = new CommonVersionResp();

            //get appId from request
            //get needed application version             
            //update whole config versions
            //update current config version            
            //select config version
            //make response

            return commonVersionResp;
        }

        /// <summary>
        /// Rolls back configuration to the version provided in request
        /// or previous version if the version parameter was omitted
        /// </summary>
        /// <param name="rollBackReq">Optional containe roll back config version</param>
        /// <returns>Containe updated config version</returns>
        public CommonVersionResp RollBack(RollBackReq rollBackReq)
        {
            CommonVersionResp commonVersionResp = new CommonVersionResp();

            //get appId from request
            //get needed application version             
            //update whole config versions
            //update current config version            
            //select config version
            //make response

            return commonVersionResp;
        }

        /// <summary>
        /// Get current versions for application config
        /// </summary>
        /// <param name="getAppVersionReq">Containe appId</param>
        /// <returns>Containe updated application config version</returns>
        public CommonAppVersionResp GetAppVersion(AppHeader getAppVersionReq)
        {
            CommonAppVersionResp commonAppVersionResp = new CommonAppVersionResp();

            //get appId
            //select app config version for current config version from DB
            //make response

            return commonAppVersionResp;
        }

        /// <summary>
        /// Get current config version
        /// </summary>
        /// <param name="getVersionReq"></param>
        /// <returns>Containe current config version</returns>
        public CommonVersionResp GetVersion(CommonEmptyReq getVersionReq)
        {
            CommonVersionResp commonVersionResp = new CommonVersionResp();
            
            //get current config version from DB
            //make response

            return commonVersionResp;
        }

        /// <summary>
        /// Get the list of all applications
        /// </summary>
        /// <param name="getAppListReq"></param>
        /// <returns>Containe the list of all applications</returns>
        public GetAppListResp GetAppList(GetAppListReq getAppListReq)
        {
            GetAppListResp getAppListResp = new GetAppListResp();

            //select all application for current config version from DB
            //make response

            return getAppListResp;
        }
    }
}